<script src="/public/static/js/jquery.min.js"></script>
<script src="/public/static/plugin/layer/layer.js"></script>
<style>
	.contain{
		margin-top: 10px;
		padding-left: 10px;
		font-size:13px;
		margin-right: 10px;
		font-family: 微软雅黑, "Source Sans Pro", sans-serif;
		width: 700px;
	}
	
    
a.ncap-btn-big { text-decoration: none;font: bold 14px/20px "microsoft yahei", arial; color: #FFFFFF; background-color: #5cb85c; text-align: center; vertical-align: middle; display: inline-block; height: 20px; padding: 7px 19px; border: solid 1px #5cb85c; border-radius: 3px; cursor: pointer; }
a:hover.ncap-btn-big { text-decoration: none; color: #FFFFFF; background-color: #5cb85c; box-shadow: 0 0 0 2px rgba(82, 168, 236, 0.15); }

	
.ncap-account-container { display: block; min-height: 20px; padding: 0px 10px 10px 40px;  }
.ncap-account-container:nth-child(even) { background: #FDFDFD; }
.ncap-account-container:hover { background: #F4FCFA; }
.ncap-account-container h4 { font-size: 13px; font-weight: normal; color: #666; text-align: right; vertical-align: top; display: inline-block; *display: inline/*IE7*/;
 margin-right: 1%; *zoom: 1;margin-left: 5px;
}
.ncap-account-container-list { font-size: 0; *word-spacing:-1px/*IE6銆�7*/;
vertical-align: top; display: inline-block; *display: inline/*IE7*/;
width: 100%; padding-left: 1%; *zoom: 1;
}
.ncap-account-container-list li { font-size: 13px; line-height: 20px; color: #999; vertical-align: top; letter-spacing: normal; word-spacing: normal; display: inline-block; *display: inline/*IE7*/;
padding-right: 30px; height: 25px; margin-bottom: 5px; *zoom: 1;
}
.bot{margin-left: 530px; margin-top: 30px;}


.alert-warning {
    color: red;
    padding: 20px 0px 0px 20px;
}
</style>

<div class="contain">
	    <{if count($data) == 0}>
	    	<div class="alert-warning">请先添加渠道</div>
	    <{else}>	
	    <div class="ncap-form-default">
			
			<dl class="row">
				
				<dd style="margin-left:0px;">
					<div class="ncap-account-container" style="height: 30px;">
						<h4>
							<input id="cls_full" onclick="choosebox(this)" type="checkbox">
							<label>全选</label>
						</h4>
					</div>
					<{foreach from=$data item=item key=key}>
						<div class="ncap-account-container" style="">
							<h4>
								<input value="1" cka="mod-<{$key}>" type="checkbox">
								<label><{$key}></label>
							</h4>
							<ul class="ncap-account-container-list">
								<{foreach from=$item item=val key=k}>
									<li>
										<label><input class="checkbox" name="channel_id[]" value="<{$val.id}>" ck="mod-<{$key}>" type="checkbox" <{if in_array($val.id, $channel_ids)}>checked="checked"<{/if}>><{$val.channel_name}></label>
									</li>
								<{/foreach}>								
							</ul>
						</div>
					<{/foreach}>	
				</dd>
			</dl>
            <input type="hidden" name="id" id="id" value="<{$id}>" />
			<div class="bot"><a href="JavaScript:void(0);"  class="ncap-btn-big" id="submitBtn" data-url="<{$url}>">确认提交</a></div>
		</div>
		<{/if}>
</div>



<script>
	$(document).ready(function(){
		$(":checkbox[cka]").click(function(){
			var $cks = $(":checkbox[ck='"+$(this).attr("cka")+"']");
			if($(this).is(':checked')){
				$cks.each(function(){$(this).prop("checked",true);});
			}else{
				$cks.each(function(){$(this).removeAttr('checked');});
			}
		});
	});
	
    function choosebox(o){
		var vt = $(o).is(':checked');
		if(vt){
			$('input[type=checkbox]').prop('checked',vt);
		}else{
			$('input[type=checkbox]').removeAttr('checked');
		}
	}
    
    var flag = true;
    $("#submitBtn").click(function(){
    	
    	var arr = new Array();
        $("input[name='channel_id[]']:checkbox[checked]").each(function(i){
            arr[i] = $(this).val();
        });
        var channel = arr.join(",");
        if(channel == ''){
        	layer.msg('请选择渠道', {icon: 2,time: 2000});
        	return false;
        }
            
        if(!flag){
    		return false;
    	}
    	flag = false;
    	$("#submitBtn").text('提交中...');
    	var index = layer.load(0, {shade: false});
        
        var url = $(this).attr('data-url');
        //console.log(url); 
        var id = $("#id").val();
    	$.ajax({
    		type : 'POST',
    		url : url,
			data : {'channel':channel,'id':id},
			dataType :'JSON',
			success : function(res){
				flag = true;
				if(res.status == 1){
					layer.close(index);
					$("#submitBtn").text('确认提交');
					layer.alert(res.msg, {
						skin: '' //样式类名
						,closeBtn: 0
					}, function(){
//						var href = window.parent.location.href;
//						var href = funcUrlDel("page");
//						console.log(href);
//						window.location.href= href; 
						window.parent.location.reload();
					});

					
				}else{
					layer.msg('操作失败', {icon: 2,time: 2000});
				}
			}
		});
    });
    
 // 删除url中某个参数,并跳转
function funcUrlDel(name){
    var loca = window.location;
    var baseUrl = loca.origin + loca.pathname + "?";
    var query = loca.search.substr(1);
    if (query.indexOf(name)>-1) {
        var obj = {}
        var arr = query.split("&");
        for (var i = 0; i < arr.length; i++) {
            arr[i] = arr[i].split("=");
            obj[arr[i][0]] = arr[i][1];
        };
        delete obj[name];
        var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
        return url;
    }else{
    	var url = baseUrl + query;
    	return url
    }
}   
</script>